package sesi.ejava.settings;

import java.util.ArrayList;
import java.util.List;
import sesi.ejava.bcam.R;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;


public class BaseSettings {
	//Nom du fichier dans lequel seront sauvegarder les settings
	public static final String SETTINGS_NAME = "SettingsFile";
	private Context context;
	private List<SettingsEntry> lsettings;
	private SettingsAdapter adapter;
	private SharedPreferences savedSettings;
	private SharedPreferences.Editor editor;

	//Constructeur privé car singleton
	private BaseSettings(Context context) {
		//Recuperation du context
		this.context = context;
		//Creation d'une liste de settings partagé pour le context
		savedSettings = context.getSharedPreferences(SETTINGS_NAME, 0);
		//Creation d'un editor pour pouvoir modifier les preferences
		editor = savedSettings.edit();
		//creation d'un tableau de settingsEntry
		lsettings = new ArrayList<SettingsEntry>();
		//creation d'un adaptateur pour l'affichage de la view
		adapter = new SettingsAdapter(context, lsettings);
		lsettings.clear();
		
		//Liste des settings disponible et leur valeur par défaut (si aucun fichier de settings n'est enregistré)
		lsettings.add(new SettingsEntry(context.getString(R.string.window)	,savedSettings.getString("nbVitres"	, "2"		)));
		lsettings.add(new SettingsEntry(context.getString(R.string.tunit)	,savedSettings.getString("tUnit"	, "Celsius"	)));
		lsettings.add(new SettingsEntry(context.getString(R.string.sunit)	,savedSettings.getString("sUnit"	, "Km/h"	)));
		lsettings.add(new SettingsEntry(context.getString(R.string.gear)	,savedSettings.getString("nbGear"	, "5"		)));
		lsettings.add(new SettingsEntry(context.getString(R.string.gaz)		,savedSettings.getString("QGaz"		, "100"		)));
		lsettings.add(new SettingsEntry(context.getString(R.string.oil)		,savedSettings.getString("QOil"		, "100"		)));
		lsettings.add(new SettingsEntry(context.getString(R.string.fog)		,savedSettings.getString("antiB"	, "Off"		)));
		lsettings.add(new SettingsEntry(context.getString(R.string.cam)		,savedSettings.getString("cam"		, "Off"		)));
		lsettings.add(new SettingsEntry("Bluetooth",""));
	}
	
	
	//accesseur
	//modificateur
	public Editor getEditor(){
		return editor;
	}

	public SharedPreferences getSharedPreferences(){
		return savedSettings;
	}

	public Context getContext(){
		return context;
	}
	public String getNbVitres(){
		return lsettings.get(0).getEntryValue();
	}

	public void setNbVitres(String nValue){
		lsettings.get(0).setEntryValue(nValue);
		adapter.notifyDataSetChanged();
		editor.putString("nbVitres", nValue);
		editor.commit();
	}

	public boolean getTUnit(){
		if(lsettings.get(1).getEntryValue() != "Celsius"){
			return false;
		}else{
			return true;
		}
	}

	public void setTUnit(String nValue){
		lsettings.get(1).setEntryValue(nValue);
		adapter.notifyDataSetChanged();
		editor.putString("tUnit", nValue);
		editor.commit();
	}

	public boolean getSUnit(){
		if(lsettings.get(2).getEntryValue() != "Km/h"){
			return false;
		}else{
			return true;
		}
	}

	public void setSUnit(String nValue){
		lsettings.get(2).setEntryValue(nValue);
		adapter.notifyDataSetChanged();
		editor.putString("sUnit", nValue);
		editor.commit();
	}

	public int getNGear(){
		return Integer.parseInt(lsettings.get(3).getEntryValue());
	}

	public void setNGear(String nValue){
		lsettings.get(3).setEntryValue(nValue);
		adapter.notifyDataSetChanged();
		editor.putString("nbGear", nValue);
		editor.commit();
	}
	//Settings no "4" Quantité d'essence
	public int getQGaz(){
		return Integer.parseInt(lsettings.get(4).getEntryValue());
	}

	public void setQGas(String nValue){
		lsettings.get(4).setEntryValue(nValue);
		adapter.notifyDataSetChanged();
		editor.putString("QGaz", nValue);
		editor.commit();
	}
	
	public int getQOil(){
		return Integer.parseInt(lsettings.get(5).getEntryValue());
	}

	public void setQOil(String nValue){
		lsettings.get(5).setEntryValue(nValue);
		adapter.notifyDataSetChanged();
		editor.putString("QOil", nValue);
		editor.commit();
	}
	
	public boolean getAntiB(){
		if(lsettings.get(6).getEntryValue() != "On"){
			return false;
		}else{
			return true;
		}
	}

	public void setAntiB(String nValue){
		lsettings.get(6).setEntryValue(nValue);
		adapter.notifyDataSetChanged();
		editor.putString("antiB", nValue);
		editor.commit();
	}

	public boolean getCam(){
		if(lsettings.get(7).getEntryValue() != "On"){
			return false;
		}else{
			return true;
		}
	}

	public void setcam(String nValue){
		lsettings.get(7).setEntryValue(nValue);
		adapter.notifyDataSetChanged();
		editor.putString("cam", nValue);
		editor.commit();
	}

	public SettingsAdapter getAdapter(){
		return adapter;
	}

	public List<SettingsEntry> getListSettings(){
		return lsettings;
	}

	//fonction permettant de récuperer une unique instance de la class BaseSettings
	public static BaseSettings getsettingsSingleton(Context context) {
		if (null == settingsSingleton) { // Premier appel	
			settingsSingleton = new BaseSettings(context);
		}
		return settingsSingleton;
	}

	private static BaseSettings settingsSingleton;
}
